package com.lxinet.jeesns.core.http;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.lxinet.jeesns.core.http.HttpRequestBuilder;

public class HttpRequest
{
  public static String get(HttpRequestBuilder httpRequestBuilder)
  {
    CloseableHttpClient httpClient = null;
    CloseableHttpResponse response = null;
    String result = "";
    try
    {
      httpClient = HttpClients.createDefault();
      
      HttpGet httpGet = new HttpGet(httpRequestBuilder.getUrl());
      
      Map<String, String> headerMap = httpRequestBuilder.getHeaders();
      for (Map.Entry<String, String> entry : headerMap.entrySet()) {
        httpGet.setHeader((String)entry.getKey(), (String)entry.getValue());
      }
      RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
      
      httpGet.setConfig(requestConfig);
      
      response = httpClient.execute(httpGet);
      int statusCode = response.getStatusLine().getStatusCode();
      if (statusCode == 200)
      {
        HttpEntity entity = response.getEntity();
        result = EntityUtils.toString(entity);
      }
      else
      {
        throw new RuntimeException("������������");
      }
      return result;
    }
    catch (Exception e)
    {
      throw new RuntimeException("������������");
    }
    finally
    {
      if (null != response) {
        try
        {
          response.close();
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
      }
      if (null != httpClient) {
        try
        {
          httpClient.close();
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
      }
    }
  }
  
  public static String post(HttpRequestBuilder httpRequestBuilder)
  {
    CloseableHttpClient httpClient = null;
    CloseableHttpResponse httpResponse = null;
    String result = "";
    
    httpClient = HttpClients.createDefault();
    
    HttpPost httpPost = new HttpPost(httpRequestBuilder.getUrl());
    
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
    
    httpPost.setConfig(requestConfig);
    
    Map<String, String> headerMap = httpRequestBuilder.getHeaders();
    for (Map.Entry<String, String> entry : headerMap.entrySet()) {
      httpPost.setHeader((String)entry.getKey(), (String)entry.getValue());
    }
    Map<String, String> paramsMap = httpRequestBuilder.getParams();
    if ((null != paramsMap) && ((paramsMap).size() > 0))
    {
      List<NameValuePair> nvps = new ArrayList();
      for (Map.Entry<String, String> entry : (paramsMap).entrySet()) {
        nvps.add(new BasicNameValuePair((String)entry.getKey(), (String)entry.getValue()));
      }
      try
      {
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, httpRequestBuilder.getCharset()));
      }
      catch (UnsupportedEncodingException e)
      {
        e.printStackTrace();
      }
    }
    try
    {
      httpResponse = httpClient.execute(httpPost);
      int statusCode = httpResponse.getStatusLine().getStatusCode();
      if (statusCode == 200)
      {
        HttpEntity entity = httpResponse.getEntity();
        result = EntityUtils.toString(entity);
      }
      else
      {
        throw new RuntimeException("������������");
      }
      return result;
    }
    catch (Exception e)
    {
      throw new RuntimeException("������������");
    }
    finally
    {
      if (null != httpResponse) {
        try
        {
          httpResponse.close();
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
      }
      if (null != httpClient) {
        try
        {
          httpClient.close();
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
      }
    }
  }
}
